package visual;


import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;

import data.MatrixTime;
import utils.kml.*;



public class KMLPrinterTime {
	
	
	
    //print a list of MatrixTime
	public static void printMap(List<MatrixTime> erlang_times, String out_file) {	
		
		try {
			
			String kml_file = new String(out_file);
			if(out_file.endsWith(".kmz"))
				kml_file = out_file.substring(0,out_file.indexOf(".kmz"))+".kml";
			
			Kml utilsKml = new Kml();
			
			
			PrintWriter out = new PrintWriter(new FileWriter(new File(kml_file)));
			utilsKml.printHeaderFolder(out, out_file.substring(out_file.indexOf("/")+1));			
			
			
			for(MatrixTime et: erlang_times) 
				printMap(et, out);
			
            utilsKml.printFooterFolder(out);
			out.close();
			
			if(out_file.endsWith(".kmz"))
				KML2KMZ.run(out_file);
			
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	//print one MatrixTime
	public static void printMap(MatrixTime erlang_time, String out_file) {	
		
		try {
			
			String kml_file = new String(out_file);
			if(out_file.endsWith(".kmz"))
				kml_file = out_file.substring(0,out_file.indexOf(".kmz"))+".kml";
			
			Kml utilsKml = new Kml();
			
			
			PrintWriter out = new PrintWriter(new FileWriter(new File(kml_file)));
			utilsKml.printHeaderFolder(out, out_file.substring(out_file.indexOf("/")+1));			
			
			 
			KMLSquare kml_square = new KMLSquare();
			
			for(int i=0; i<erlang_time.getMat().length; i++)
			for(int j=0; j<erlang_time.getMat()[i].length;j++) {
				if(erlang_time.getMat()[i][j] < 0 || erlang_time.getMat()[i][j] > VISIBILITY_THRESHOLD) 
					out.println(printSquare(erlang_time, i, j, kml_square));
			}
			
            utilsKml.printFooterFolder(out);
			out.close();
			
			if(out_file.endsWith(".kmz"))
				KML2KMZ.run(out_file);
			
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static final SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
	
	private static final double VISIBILITY_THRESHOLD = 0.1;
	private static void printMap(MatrixTime mt, PrintWriter out) throws Exception {	
		
		out.println("<Folder>");
		out.println("<name>"+FORMATTER.format(mt.getStart().getTime())+"-"+FORMATTER.format(mt.getEnd().getTime())+"</name>");
		out.println("<drawOrder>2</drawOrder>");
		out.println("<TimeSpan>");
		out.println("<begin>"+FORMATTER.format(mt.getStart().getTime())+"</begin>");
		out.println("<end>"+FORMATTER.format(mt.getEnd().getTime())+"</end>");
		out.println("</TimeSpan>");
		
		KMLSquare kml_square = new KMLSquare();
		//KMLCircle kml_circle = new KMLCircle();
		
		for(int i=0; i<mt.getMat().length; i++)
		for(int j=0; j<mt.getMat()[i].length;j++) {
			if(mt.getMat()[i][j] < 0 || mt.getMat()[i][j] > VISIBILITY_THRESHOLD) 
				out.println(printSquare(mt, i, j, kml_square));
				//out.println(printCircle(mt, i, j, kml_circle));
		}
		out.println("</Folder>");
	}
	
	private static String printSquare(MatrixTime mt, int i, int j, KMLSquare kml_square) {
		double[][] ll = mt.getArea().getCellBorder(i, j);
		double val = mt.getVal(i,j);
		String name = "("+i+","+j+") = "+val;
		return kml_square.draw(ll, name, color(val),color(val), "");
	}
	
	private static String printCircle(MatrixTime mt, int i, int j, KMLCircle kml_circle) {
		
		
		double[] center = mt.getArea().getCenter(i, j);
		double val = mt.getVal(i,j);
		
		double radius = 250;
		String name = "("+i+","+j+") = "+val;
		
		return kml_circle.draw(center[0], center[1], radius, 30, name,color(val),"");
	}
	
	private static String color(double val) {
		// get color
		String red, green, blue;
		if(val >= 0) {
			int pos = (int)Math.floor((val * 255.0)); 
			red = Integer.toHexString(pos);
			if(red.length()==1) red = "0"+red;
			blue = Integer.toHexString(255 - pos);
			if(blue.length()==1) blue = "0"+blue;
			green = "00";
		}
		else {
			red = "00";
			blue = "00";
			green = "ff";
		}
		
		return "aa"+blue+green+red;
	}
	
	
}
